<?php

/**
 * MinimunCMS
 *
 * Modulo de Memorias Telefonicas
 * Muestra listado de memorias telefonicas
 *
 * @link http://www.minimuncms.com.ar
 * @copyright 2010 Sergio Sambataro
 * @author Sergio Sambataro <scs.1975@gmail.com>
 * @package minimuncms
 * @version 2.0
*/

class Memorias_empresa1 extends Controller {

    function Memorias_empresa1() {
        parent::Controller();
        $this->load->model('admin/modelo_modulos');
        if(!$this->session->userdata('logged_in') || $this->modelo_modulos->get_permisos('memorias_empresa1_principal','empresa1')=='no')
            redirect('/home/');
        $this->load->library('session');
        $this->load->language('general',$this->session->userdata('lenguaje'));
        $this->load->language('memorias_empresa1',$this->session->userdata('lenguaje'));
    }

    function index() {
        $datos['titulo_pagina'] = 'Memorias Telefonicas';
        $this->load->view('encabezado', $datos);
        $this->modelo_modulos->get_modulos();
        $this->load->view('menu');
        $this->load->view('memorias_empresa1');
        $this->load->view('pie');
    }


    function post() {
        $tabla       = "min_memorias_empresa1";
        $page        = $this->input->post('page');
        $limit       = $this->input->post('rows');
        $sidx        = $this->input->post('sidx');
        $sord        = $this->input->post('sord');
        $_search     = $this->input->post('_search');
        $searchField = $this->input->post('searchField');
        $searchString= $this->input->post('searchString');

        $where = "";
        if ($_search=='true')
            $where = " WHERE $searchField LIKE '%$searchString%' ";

        if(!$sidx) $sidx =1;

        $sql    = "SELECT * FROM $tabla $where";
        $query  = $this->db->query($sql);
        $result = $query->result();
        $count  = count($result);

        if( $count > 0 && $limit > 0) {
            $total_pages = ceil($count/$limit);
        } else {
            $total_pages = 0;
        }

        if ($page > $total_pages) $page=$total_pages;
        $start = $limit*$page - $limit;
        if($start <0) $start = 0;

        $sql    = "SELECT * FROM $tabla $where ORDER BY $sidx $sord LIMIT $start , $limit";
        $query  = $this->db->query($sql);

        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
        header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
        header("Cache-Control: no-cache, must-revalidate" );
        header("Pragma: no-cache" );
        header("Content-type: text/x-json");
        $i=0;
        $data['page']= $page;
        $data['total']= $total_pages;

        foreach ($query->result() as $row) {
            $data['rows'][$i]['id']= $row->id;
            $data['rows'][$i]['cell'][]= $row->id;
            $data['rows'][$i]['cell'][]= $row->nombre;
            $data['rows'][$i]['cell'][]= utf8_encode($row->telefono);
            $data['rows'][$i]['cell'][]= utf8_encode($row->email);
            $i++;
        }
        echo json_encode($data);
    }

    function guardar() {
        $tabla    = 'min_memorias_empresa1';
        $oper     = $this->input->post('oper');
        $id       = $this->input->post('id');
        $nombre   = $this->input->post('nombre');
        $telefono = $this->input->post('telefono');
        $email    = $this->input->post('email');
        if($oper=='edit') {
            $query = $this->db->query("UPDATE $tabla set nombre='".$nombre."', telefono='".$telefono."', email='".$email."' WHERE id=".$id);
        }elseif($oper=='add') {
            $query = $this->db->query("INSERT INTO $tabla set nombre='".$nombre."', telefono='".$telefono."', email='".$email."'");
        }elseif($oper=='del') {
            $query = $this->db->query("DELETE FROM $tabla WHERE id=$id");
        }
    }

}
?>